
package com.zzvcom.sms.utils;

import org.apache.http.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

public class HttpUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtil.class);
    private static CloseableHttpClient httpClient = null;
    private static final int COUNT = Integer.parseInt(PropUtil.getProp("defaultMaxPerRoute", "20"));
    private static final int MAX_TOTAL = Integer.parseInt(PropUtil.getProp("maxTotal", "100"));
    private static final int KEEP_ALIVE_TIME = Integer.parseInt(PropUtil.getProp("keepAliveTime", "10000"));
    public static ConnectionKeepAliveStrategy defaultStrategy = new ConnectionKeepAliveStrategy() {
        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator("Keep-Alive"));

            while (true) {
                String param;
                String value;
                do {
                    do {
                        if (!it.hasNext()) {
                            return HttpUtil.KEEP_ALIVE_TIME * 1000L;
                        }

                        HeaderElement he = it.nextElement();
                        param = he.getName();
                        value = he.getValue();
                    } while (value == null);
                } while (!param.equalsIgnoreCase("timeout"));

                try {
                    return Long.parseLong(value) * 1000L;
                } catch (Exception var9) {
                    var9.printStackTrace();
                    HttpUtil.LOGGER.error("format KeepAlive timeout exception, exception:" + var9.toString());
                }
            }
        }
    };

    private HttpUtil() {
    }

    public static synchronized void initPools() {
        if (httpClient == null) {
            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
            cm.setDefaultMaxPerRoute(COUNT);
            cm.setMaxTotal(MAX_TOTAL);
            httpClient = HttpClients.custom().setKeepAliveStrategy(defaultStrategy).setConnectionManager(cm).build();
        }

    }

    public static CloseableHttpClient getHttpClient() {
        if (httpClient == null) {
            initPools();
        }

        return httpClient;
    }

    public static String post(String url) {
        LOGGER.info("url: " + url);
        CloseableHttpClient httpClient = getHttpClient();
        HttpPost httpPost = new HttpPost(url);
        String responseBody = null;
        CloseableHttpResponse response = null;

        try {
            response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                responseBody = EntityUtils.toString(responseEntity);
            }
        } catch (IOException | ParseException var18) {
            var18.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException var17) {
                var17.printStackTrace();
            }

        }

        return responseBody;
    }

    public static String buildQueryString(Map<String, String> map, String key, String value) {
        StringBuilder sb = new StringBuilder();

        for (Map.Entry<String, String> s : map.entrySet()) {
            sb.append(s.getKey()).append("=").append(s.getValue()).append("&");
        }

        sb.append(key).append("=").append(value);
        return sb.toString();
    }
}